package com.liao.badminton.code.util;

import com.alibaba.fastjson.JSON;
import com.liao.badminton.code.ResponseRest;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.servlet.ServletResponse;
import java.io.PrintWriter;

/**
 * author liaocheng
 * description :
 * DATE: 2021/2/4
 */
public class ResponseUtils {
    static Logger logger = LoggerFactory.getLogger(ResponseUtils.class);

    /**
     * 使用response输出JSON
     *
     * @param response
     * @param result
     */
    public static void out(ServletResponse response, ResponseRest result) {
        PrintWriter out = null;
        try {
            response.setCharacterEncoding("UTF-8");
            response.setContentType("application/json");
            out = response.getWriter();
            out.write(JSON.toJSONString(result));
        } catch (Exception e) {
            logger.error(e + "输出JSON出错");
        } finally {
            if (out != null) {
                out.flush();
                out.close();
            }
        }
    }


}
